package org.xshop.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xshop.model.Member;
import org.xshop.model.MemberPhoto;
import org.xshop.model.TMember;
import org.xshop.model.TParam;
import org.xshop.service.ITMemberService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.support.Assert;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.SecurityUtil;
import top.ibase4j.core.util.UploadUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 会员 前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2017-03-19
 */
@Controller
@RequestMapping("/app/member/")
@Api(tags = "MemberInformation", value = "会员管理接口", description = "APP-个人中心-个人信息管理接口")
public class TMemberController extends AppBaseController<TMember, ITMemberService> {
	@ApiOperation(value = "获取个人基本信息", notes = "获取其他用户的个人信息", produces = MediaType.APPLICATION_JSON_VALUE, response = TMember.class)
	@RequestMapping(value = "getUserBaseInfo.api", method = { RequestMethod.GET, RequestMethod.POST })
	public Object getBaseInfo(HttpServletRequest request, TParam param) {
		param = WebUtil.getParameter(request, TParam.class);
		Assert.notNull(param.getId(), "ID");
		Object result = service.getBaseInfo(param.getId());
		return setSuccessModelMap(result);
	}

	@ApiOperation(value = "获取个人信息", produces = MediaType.APPLICATION_JSON_VALUE, notes = "获取当前用户的个人信息", response = TMember.class)
	@RequestMapping(value = "getUserInfo.api", method = { RequestMethod.GET, RequestMethod.POST })
	public Object get(HttpServletRequest request, TParam param) {
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setId(id);
		}
		Assert.notNull(param.getId(), "ID");
		TMember result = service.getInfo(param.getId());
		result.setPassword(null);
		result.setToken(null);
		return setSuccessModelMap(result);
	}

	@PostMapping("modifyUserInfo.api")
	@ApiOperation(value = "修改个人信息", produces = MediaType.APPLICATION_JSON_VALUE, response = TMember.class, notes = "修改当前用户的个人信息")
	public Object update(HttpServletRequest request, Member member) {
		member = WebUtil.getParameter(request, Member.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			member.setId(id);
		}
		Assert.notNull(member.getId(), "ID");
		TMember param = InstanceUtil.to(member, TMember.class);
		TMember user = service.queryById(param.getId());
		Assert.notNull(user, "MEMBER", param.getId());
		if (StringUtils.isNotBlank(param.getPassword())) {
			if (!param.getPassword().equals(user.getPassword())) {
				param.setPassword(SecurityUtil.encryptPassword(param.getPassword()));
			}
		}
		return super.update(request, new ModelMap(), param);
	}

	@PostMapping("uploadPhoto.api")
	@ApiOperation(value = "修改个人头像", notes = "更新当前用户的头像", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object uploadPhoto(HttpServletRequest request, MemberPhoto param) {
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setMemberId(id);
		}
		Assert.notNull(param.getMemberId(), "ID");
		List<String> avatars = UploadUtil.uploadImage(request, false);
		org.springframework.util.Assert.notEmpty(avatars, "头像数据dataFile不能为空");
		TMember member = new TMember();
		member.setId(param.getMemberId());
		TMember user = service.queryById(member.getId());
		Assert.notNull(user, "MEMBER", member.getId());
		String filePath = UploadUtil.getUploadDir(request) + avatars.get(0);
		String avatar = UploadUtil.remove2FDFS(filePath).getRemotePath();
		member.setAvatar(avatar);
		Long userId = getCurrUser(request);
		member.setUpdateBy(userId);
		member.setUpdateTime(new Date());
		service.update(member);
		Map<String, Object> result = InstanceUtil.newHashMap("bizeCode", 1);
		result.put("avatar", avatar);
		return setSuccessModelMap(result);
	}

	@PostMapping("shareReturn.api")
	@ApiOperation(value = "分享回调", produces = MediaType.APPLICATION_JSON_VALUE, response = TMember.class, notes = "修改当前用户的个人信息")
	public Object share(HttpServletRequest request, Member member) {
		member = WebUtil.getParameter(request, Member.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			member.setId(id);
		}
		Assert.notNull(member.getId(), "ID");
		TMember param = InstanceUtil.to(member, TMember.class);
		TMember user = service.queryById(param.getId());
		Assert.notNull(user, "MEMBER", param.getId());
		if (user.getShareTimes() != null) {
			param.setShareTimes(user.getShareTimes() + 1);
		} else {
			param.setShareTimes(1L);
		}
		return super.update(request, new ModelMap(), param);
	}

}
